<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header">
</head>
<body class="hold-transition skin-blue sidebar-mini">
<!--详情-->
<div class="modal fade" id="modal-info">
    <div class="modal-dialog" style="width:100%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">预购计划审核</h4>
            </div>
            <div class="modal-body">
                <section class="content" style="padding: 5px!important;">
                    <div id="div_review" style="margin-bottom: 10px!important;height: 42px;">

                    </div>
                    <div class="row">
                        <form id="infoPreOrderPlanForm" class="form-horizontal">
                            <div class="col-xs-12"
                                 style="max-width: 100%;max-height: 80vh;overflow: auto;padding: 0!important;">
                                <input type="hidden" name="reportType" value="1">
                                <table id="table_import_info" class="t1 t3">
                                    <thead>
                                    <tr>
                                        <td colspan="12" style="font-size: 16px;letter-spacing: 4px;">
                                            预购计划单
                                        </td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" style="width: 8%">主题</td>
                                        <td colspan="7" style="width: 11%;font-size: 14px;">
                                            <input type="text" class="form-control" name="name"
                                                   placeholder="请输入预购主题"/>
                                        </td>
                                        <td style="width: 8%">预购编号</td>
                                        <td colspan="2" style="width: 21%">
                                            <input style="background: #FFFFFF" type="text" class="form-control"
                                                   name="code" readonly="readonly"/>
                                        </td>

                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td style="width: 3%">序号</td>
                                        <td colspan="3">物料描述</td>
                                        <td style="width: 8%">物料编码</td>
                                        <td style="width: 4%">预购单位</td>
                                        <td style="width: 6%">预购数量</td>
                                        <td style="width: 6%">业务员</td>
                                        <td style="width: 10%">预购备注</td>
                                        <td style="width: 6%">品牌</td>
                                        <td style="width: 8%">预购项目号</td>
                                        <td style="width: 12%">预购项目名</td>
                                        <td></td>
                                    </tr>
                                    </thead>
                                    <tbody id="outForInfo">
                                    <tr>
                                        <td>1</td>
                                        <td colspan="3">
                                            <input type="text" class="form-control" name="materialDesc"
                                                   autocomplete="off" maxlength="255"/>
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="materialCode" autocomplete="off"
                                                   maxlength="255">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="unit" autocomplete="off"
                                                   maxlength="255">
                                        </td>
                                        <td>
                                            <input type="number" class="form-control" name="num" autocomplete="off">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="purchaser" autocomplete="off"
                                                   maxlength="255">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="preOrderRemark"
                                                   autocomplete="off" maxlength="255">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="brand" autocomplete="off"
                                                   maxlength="255">
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="proCode" autocomplete="off"
                                                   maxlength="255"/>
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="proName" autocomplete="off"
                                                   maxlength="255">
                                        </td>
                                        <td>
                                            <label class="badge label-default" name="uploadFileText">无附件</label>
                                            <input type="hidden" class="form-control" name="id" readonly="readonly"/>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </form>
                    </div>
                    <!--                    <div th:include="include :: tip"></div>-->
                </section>
            </div>
        </div>
    </div>
</div>
<section class="content-header"><h1>预购计划审核</h1></section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" method="get">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>预购主题</label>
                                    <input type="text" name="name" class="form-control"
                                           placeholder="请输入预购主题">
                                </div>
                                <div class="form-group">
                                    <label>预购编号</label>
                                    <input type="text" name="code" class="form-control"
                                           placeholder="请输入预购编号">
                                </div>
                                <div class="form-group">
                                    <label>申请人</label>
                                    <input type="text" name="applicant" class="form-control" placeholder="请输入申请人">
                                </div>
                                <div class="form-group">
                                    <label>业务员</label>
                                    <input type="text" name="purchaser" class="form-control"
                                           placeholder="请输入业务员">
                                </div>
                                <div class="form-group">
                                    <label>收货状态</label>
                                    <select name="receiptStatus" class="form-control"
                                            style="padding: 0px!important;text-align: center!important;text-align-last: center!important;width:auto!important;">
                                        <option value="" selected="selected">全部</option>
                                        <option value="1">未收货</option>
                                        <option value="2">部分收货</option>
                                        <option value="3">无需收货</option>
                                        <option value="4">收货完成</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>审核状态</label>
                                    <select name="rejectStatus" class="form-control"
                                            style="padding: 0px!important;text-align: center!important;text-align-last: center!important;width:auto!important;">
                                        <option value="">请选择</option>
                                        <option value="1" selected>未审核</option>
                                        <option value="2">已审核</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>预购日期</label>
                                    <input type="text" class="form-control datepicker" name="createTimeStartTime"
                                           readonly="readonly"/>
                                    <span> 至 </span>
                                    <input type="text" class="form-control datepicker" name="createTimeEndTime"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>

                        </div>
                    </form>
                    <div class="row" id="toolbar">
                        <div class="form-inline">
                            <a shiro:hasPermission="buy:preorderplancheck:check" href="javascript:void(0);"
                               class="btn btn-danger pull-right" style="margin-right: 10px" onclick="rejectAll()">
                                <span class="Bold">批量驳回</span>
                            </a>
                            <a shiro:hasPermission="buy:preorderplancheck:check" href="javascript:void(0);"
                               class="btn btn-success pull-right" style="margin-right: 10px" onclick="successAll()">
                                <span class="Bold">批量审核</span>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    let table;
    let receiptStatusState = ['', "未收货", "部分收货", "无需收货", "收货完成"];
    let receiptStatusColors = ['', "label-info", "label-warning", "label-primary", "label-success"];
    /**
     * 批量驳回
     */
    function rejectAll() {
        js.modal.prompt("请编辑驳回原因", function (reason) {
            if(reason === null){
                return;
            }
            let selectedRows = table.bootstrapTable('getSelections');
            const selectedCodes = selectedRows.map(row => {
                if (row.rejectStatus === 1) {
                    return row.code;
                }
            });
            if(selectedCodes.length > 0 ){
                doReject(selectedCodes,reason);
            }
        });
    }

    /**
     * 批量审核
     */
    function successAll() {
        let selectedRows = table.bootstrapTable('getSelections');
        const selectedCodes = selectedRows.map(row => {
            if (row.rejectStatus === 1) {
                return row.code;
            }
        });
        let newSelectedCodes = [];
        for (let i = 0; i < selectedCodes.length; i++) {
            if (selectedCodes[i] !== undefined) {
                newSelectedCodes.push(selectedCodes[i]);
            }
        }
        if(newSelectedCodes !== undefined && newSelectedCodes !== null && newSelectedCodes.length > 0) {
            let codesStr = '';
            for (let i = 0; i < newSelectedCodes.length; i++) {
                codesStr += "【"+newSelectedCodes[i]+"】,";
            }
            codesStr = codesStr.substring(0, codesStr.length - 1);
            js.modal.confirm("您确认要审核通过预购编码为"+codesStr+"的预购计划吗？", function () {
                doSuccess(newSelectedCodes);
            });
        }
    }
    /**
     * 审核通过 单审
     * @param obj 按钮对象
     */
    function success(obj){
        let code = $(obj).attr('sheetCode');
        js.modal.confirm("确认通过吗？", function () {
            doSuccess([code]);
        })
    }
    /**
     * 执行审核通过
     * @param codes 审核通过的预购编号
     */
    function doSuccess(codes){
        js.post({
            url: ctx + "buy/preOrderPlanCheck/success",
            data: {codes: codes},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    location.reload();
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }
    /**
     * 驳回 单审
     * @param obj 按钮对象
     */
    function reject(obj){
        let code = $(obj).attr('sheetCode');
        js.modal.prompt("请编辑驳回原因", function (reason) {
            if (reason === null) {
                return;
            }
            doReject([code],reason);
        });
    }

    /**
     * 执行驳回
     * @param codes 被驳回的预购单编号数组
     * @param reason 驳回原因
     */
    function doReject(codes,reason) {
        js.post({
            url: ctx + "buy/preOrderPlanCheck/reject",
            data: {
                codes: codes,
                rejectReason: reason
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    location.reload();
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }
    /**
     * 搜索
     */
    function search() {
        js.table.refresh(table);
    }
    /**
     * 下载附件
     * @param id 附件所属数据行ID
     * @param fileNameTitle 文件名
     */
    function downloadFile(id, fileNameTitle) {
        if(hasP('buy:preorderplancheck:download')){
            if (fileNameTitle !== undefined && fileNameTitle !== null) {
                js.post({
                    url: ctx + "buy/preOrderPlanCheck/getAttachmentIsExist",
                    data: {id: id},
                    success: function (result) {
                        //如果文件存在
                        if (result.msg == "有文件") {
                            //下载文件
                            location.href = ctx + "buy/preOrderPlanCheck/downloadAttachment/" + id;
                        } else {
                            js.modal.warning("没有附件可以下载！");
                        }
                    }
                });
            }
        }else{
            js.modal.warning("你没有下载权限！");
        }
    }
    /**
     * 打开审核模态框
     * @param code 预购编号
     * @param behavior 行为
     */
    function openInfoModal(code,behavior) {
        js.post({
            url: ctx + "buy/preOrderPlanCheck/getProOrderPlanListByCode",
            data: {code: code},
            success: function (result) {
                let list = result.data;
                $('#infoPreOrderPlanForm input[name="name"]').val(list[0].name);
                $('#infoPreOrderPlanForm input[name="code"]').val(list[0].code);
                $('#btn-success').attr("sheetCode",list[0].code);
                $('#btn-reject').attr("sheetCode",list[0].code);
                $("#outForInfo").empty();
                for (let i = 0; i < list.length; i++) {
                    let fileClass = 'label-default';
                    let fileStatus = '无附件';
                    let fileNameTitle = '';
                    if (list[i].attachmentName !== undefined && list[i].attachmentName !== null && list[i].attachmentName !== '') {
                        fileClass = 'label-success';
                        fileStatus = '有附件';
                        fileNameTitle = 'title = ' + list[i].attachmentName + '（点击下载）';
                    }
                    let no = i + 1;
                    $("#outForInfo").append(`<tr>
                                        <td>${no}</td>
                                        <td colspan="3">
                                            <input type="text" class="form-control" name="materialDesc" autocomplete="off" maxlength="255" value="${list[i].materialDesc}" />
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="materialCode" autocomplete="off" maxlength="255" value="${list[i].materialCode}" />
                                        </td>
                                         <td>
                                            <input type="text" class="form-control" name="unit" autocomplete="off" maxlength="255" value="${list[i].unit}" />
                                        </td>
                                        <td>
                                            <input type="number" class="form-control" name="num" autocomplete="off" value="${list[i].num}" />
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="purchaser" autocomplete="off" maxlength="255" value="${list[i].purchaser}" />
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="preOrderRemark" autocomplete="off" maxlength="255" value="${list[i].preOrderRemark}" />
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="brand" autocomplete="off" maxlength="255" value="${list[i].brand}" />
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="proCode" autocomplete="off" maxlength="255" value="${list[i].proCode}" />
                                        </td>
                                        <td>
                                            <input type="text" class="form-control" name="proName" autocomplete="off" maxlength="255" value="${list[i].proName}" />
                                        </td>
                                        <td>
                                            <label onclick="downloadFile(${list[i].id},\'${fileNameTitle}\')" class="badge ${fileClass}" name="uploadFileText" ${fileNameTitle}>${fileStatus}</label>
                                            <input type="hidden" class="form-control" name="id" readonly="readonly" value="${list[i].id}" />
                                        </td>
                                    </tr>`);
                }
                if(behavior === '审核'){
                    $("#div_review").html('<button shiro:hasPermission="buy:preorderplancheck:check" id="btn-reject" class="btn btn-danger pull-right" style="margin-left: 20px;" sheetCode="'+code+'" onclick="reject(this)">驳回</button>\n' +
                        '                        <button shiro:hasPermission="buy:preorderplancheck:check" id="btn-success" class="btn btn-success pull-right" style="margin-left: 20px;" sheetCode="'+code+'" onclick="success(this)">同意</button>');
                }else{
                    $("#div_review").html('');
                }
                js.modal.open("modal-info");
            }
        })
    }
    $(function () {
        /** 提报计划表 */
        table = js.table.init({
            url: ctx + "buy/preOrderPlanCheck/list",
            paginationVAlign: 'both',
            pageSize: 15,
            pageList: [10, 15, 25, 50, 100, 300, 1000],               // 可供选择的每页的行数
            showColumns: true,
            showExport: false,
            showRefresh: true,
            clickToSelect: true,
            uniqueId: "code",
            onLoadSuccess: function (){
                $('#btn-search').prop('disabled', false).removeClass('disabled');
            },
            columns: [
                {
                    checkbox: true,
                    width: '30px',
                    formatter: function (value, row, index) {
                        if (row.cur === 1) {
                            return {disabled: false}
                        } else {
                            return {disabled: true}
                        }
                    }
                },
                {
                    title: '序号', width: '50px',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {
                    title: "预购主题", field: 'name',
                    formatter: function (value, row, index, field) {
                        if (value && value.length > 50) {
                            return '<span title="' + value + '">' + value.substring(0, 50) + '</span> ';
                        } else {
                            return value;
                        }
                    }
                },
                {
                    title: "预购编号", field: 'code', width: '170px'
                },
                {
                    title: "预购日期", field: 'createTime', width: '150px'
                },
                {title: "审核状态", field: 'rejectStatus',
                    formatter: function (value) {
                        if (value === 2) {
                            return '<span class="badge label-success">已审核</span>';
                        } else {
                            return '<span class="badge label-warning">未审核</span>';
                        }
                    }
                },
                {title: "审核人", field: 'verifyName1',
                    formatter: function (value, row, index) {
                        if (row.rejectStatus === 2) {
                            return value;
                        }
                        return "-";
                    }},
                {title: "驳回原因", field: 'rejectReason', width: '100px'},
                {
                    title: "收货状态",
                    field: 'receiptStatus',
                    width: '100px',
                    formatter: function (value, row, index) {
                        if (value) {
                            return '<span class="badge ' + receiptStatusColors[value] + '">' + receiptStatusState[value] + '</span> ';
                        }
                    }
                },
                {title: "申请人", field: 'applicant', width: '100px'},
                {title: "业务员", field: 'purchaser', width: '160px'},
                {
                    title: "操作",
                    width: '240px',
                    formatter: function (value, row, index) {
                        var actions = [];
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="openInfoModal(\'' + row.code + '\',\'查看\')"> 查看</a> ');
                        if (hasP('buy:preorderplancheck:view') && row.rejectStatus === 1 && row.cur === 1) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="openInfoModal(\'' + row.code + '\',\'审核\')"> 审核</a> ');
                        }
                        return actions.join('');
                    }
                }
            ]
        });
        /** 表格搜索 */
        $("#btn-search").click(function () {
            $('#btn-search').prop('disabled', true).addClass('disabled');
            js.table.search(table);
        });
    });



</script>
</body>
</html>